<?php
/**
 * 分页类
 * @author Jim
 * $params = array(
 *      'baseurl' => '', 连接文件名
 *      'total' => 0,  记录总数
 *      'pagesize' => 10, 每页显示记录数
 *      'next' => '下一页',
 *      'prev' => '上一页', 
 *      'page' => 1, 当前页码
 *      'display' => 5, 显示多少个页码
 *      'type' => 1,  显示类型，1只显示上一页，下一页，不显示页码；2显示页码与上下页
 * )
 */

class Pagination
{
    private $baseurl = '';
    private $params = array();
    private $htmlt = "共<font color=red>%d</font>条记录，当前第<font color=red>%d / %d</font>页 ";
    private $next = "下一页";
    private $prev = "上一页";
    private $first = '第一页';
    private $last = '最后一页';
    private $page = 1;
    private $display = 5;
    private $type = 1;
    private $total = 0;
    private $pagesize = 10;
    private $pagecount = 0;
    private $offset = 0;


    public function __construct($params = array()) {
        if($params)
        {
            foreach ($params as $key => $v)
            {
                $this->$key = $v;
            }
        }
        $this->pagecount = ceil($this->total/$this->pagesize);
        if(!isset($params['page']))
        {
            $this->page = isset ($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
        }
        $this->page = $this->page <= 0 ? 1 : (($this->page > $this->pagecount) ? $this->pagecount : $this->page);
        $this->offset = (($this->page - 1) > 0 ? ($this->page - 1) : 0) * $this->pagesize;
        $this->baseurl = !empty ($this->baseurl) ? $this->baseurl : preg_replace('/([&|?]page=)(.*)/','',  $this->getByurl());
        $this->baseurl .= strpos($this->baseurl, "?") > 0 ? '&page=%d' : '?page=%d' ;
    }
    
    public function filter()
    {
        switch ($this->type)
        {
            case 1:
                return $this->casual();
                break;
            case 2:
                return $this->complex();
                break;
            default :
                return $this->casual();
        }
    }
    
    public function casual()
    {
        $html = array();
        $html['header'] = sprintf($this->htmlt, $this->total, $this->page, $this->pagecount);
        $html['body']['first']['name'] = $this->first;
        $html['body']['prev']['name'] = $this->prev;
        $html['body']['next']['name'] = $this->next;
        $html['body']['last']['name'] = $this->last;
        if($this->page == 1)
        {
            $html['body']['prev']['url'] = '';
            unset($html['body']['first']);
        }elseif($this->page > 1 && $this->page <= $this->pagecount){
            $html['body']['prev']['url'] = sprintf($this->baseurl,  ($this->page-1));
            $html['body']['first']['url'] = sprintf($this->baseurl,  1);
        }
        
        if($this->page == $this->pagecount)
        {
            $html['body']['next']['url'] = '';
            unset($html['body']['last']);
        }elseif($this->page < $this->pagecount){
            $html['body']['next']['url'] = sprintf($this->baseurl,  ($this->page+1));
            $html['body']['last']['url'] = sprintf($this->baseurl, $this->pagecount);
        }
        
        return $html;
    }
    
    public function complex()
    {
        $html = array();
        $offset = 0;
        $html['header'] = sprintf($this->htmlt, $this->total, $this->page, $this->pagecount);
        if($this->pagecount <= $this->display) //总页数小于或等于显示页数
        {
            $first_number = 1;
            $last_number = $this->pagecount; 
        }else{ //总页数大于显示页数
            $offset = (int) floor( $this->display / 2 ); 
            if($this->page - $offset <= 1) //当前页
            {
                $first_number = 1;
            }elseif(($this->page + $offset) > $this->pagecount) {
                $first_number = $this->pagecount - $this->display + 1;
            }else {
                $first_number = $this->page - $offset;
            }
            $last_number = $first_number + $this->display - 1;  
        }
        if( $first_number > 1)
        {
            $html['body']['first']['name'] = '1';
            $html['body']['first']['url'] = ($this->page == 1) ? '' : sprintf($this->baseurl, 1);
            $html['body']['second']['name'] = '...';
            $html['body']['second']['url'] = '';
        }
        
        for($i = $first_number; $i <= $last_number; $i++)
        {
            if($this->page == $i)
            {
                $html['body'][$i]['url'] = '';
                $html['body'][$i]['name'] = $i;
            }else {
                $html['body'][$i]['url'] = sprintf($this->baseurl, $i);
                $html['body'][$i]['name'] = $i;
            }
        }
        if( $last_number < $this->pagecount )
        {
            $html['body']['clip']['name'] = '...';
            $html['body']['clip']['url'] = '';
            $html['body']['last']['name'] = $this->pagecount;
            $html['body']['last']['url'] = ($this->page == 1) ? '' : sprintf($this->baseurl, $this->pagecount);
        }
        return $html;
    }


    public function getByurl() {
		$sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
		$php_self = $_SERVER['PHP_SELF'] ? $this->safe_replace($_SERVER['PHP_SELF']) : $this->safe_replace($_SERVER['SCRIPT_NAME']);
		$path_info = isset($_SERVER['PATH_INFO']) ? $this->safe_replace($_SERVER['PATH_INFO']) : '';
		$relate_url = isset($_SERVER['REQUEST_URI']) ? $this->safe_replace($_SERVER['REQUEST_URI']) : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$this->safe_replace($_SERVER['QUERY_STRING']) : $path_info);
		return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
    }
    
    /**
     * 安全过滤函数
     *
     * @param $string
     * @return string
     */
    public function safe_replace($string) {
        $string = str_replace('%20','',$string);
        $string = str_replace('%27','',$string);
        $string = str_replace('%2527','',$string);
        $string = str_replace('*','',$string);
        $string = str_replace('"','&quot;',$string);
        $string = str_replace("'",'',$string);
        $string = str_replace('"','',$string);
        $string = str_replace(';','',$string);
        $string = str_replace('<','&lt;',$string);
        $string = str_replace('>','&gt;',$string);
        $string = str_replace("{",'',$string);
        $string = str_replace('}','',$string);
        return $string;
    }
    
    function __get($name) {
        return $this->$name;
    }
    
    function __set($name, $value) {
        $this->$name = $value;
    }
}